package org.light.complexverb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.generator.NamedStatementListGenerator;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class ListMyAvailableActive extends TwoDomainVerb{

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;
	}
	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.capFirst(this.getVerbName()));
		method.setReturnType(new Type("[] "+this.slave.getDomainSuffix()+this.slave.getType()));
		method.addSignature(new Signature(1,this.master.getLowerFirstDomainName()+"Id", this.master.getDomainId().getClassType()));
		method.setThrowException(true);
		return method;
	}
	@Override
	public Method generateServiceImplMethod() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.capFirst(this.getVerbName()));		
		method.setReturnType(new Type("[] "+this.slave.getDomainSuffix()+"."+this.slave.getType()));
		method.addSignature(new Signature(1,this.master.getLowerFirstDomainName()+"Id", this.master.getDomainId().getClassType()));
		
		ListMyActive listMa = new ListMyActive(this.master,this.slave);
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,1,"db := database.DB"));
		sList.add(new Statement(2000L,1,"all"+this.slave.getCapFirstPlural()+" := "+this.master.getDaoimplSuffix()+".ListActive"+this.slave.getCapFirstPlural()+"(db)"));
		sList.add(new Statement(3000L,1,"has"+this.master.getCapFirstDomainName()+" := "+this.master.getDaoimplSuffix()+"."+listMa.getVerbName()+"(db, "+this.master.getLowerFirstDomainName()+"Id)"));
		sList.add(new Statement(4000L,1,"retVals := [] "+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainNameWithSuffix()+"{}"));
		sList.add(new Statement(5000L,0,""));
		sList.add(new Statement(6000L,1,"for _, v := range all"+this.slave.getCapFirstPlural()+" {"));
		sList.add(new Statement(7000L,2,"exists := existsIn"+this.slave.getCapFirstDomainName()+"Arr(has"+this.master.getCapFirstDomainName()+", v)"));
		sList.add(new Statement(8000L,2,"if !exists {"));
		sList.add(new Statement(9000L,3,"retVals = append(retVals, v)"));
		sList.add(new Statement(10000L,2,"}"));
		sList.add(new Statement(11000L,1,"}"));
		sList.add(new Statement(12000L,1,"return retVals"));
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}
	@Override
	public Method generateControllerMethod() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.capFirst(this.getVerbName()));		
		method.addSignature(new Signature(1,"c","*gin.Context"));
		
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,1,"idStr := c.PostForm(\""+this.master.getLowerFirstDomainName()+"Id\")"));
		sList.add(new Statement(2000L,1,this.master.getLowerFirstDomainName()+"Id, _ := strconv.ParseInt(idStr, 10, 64)"));
		sList.add(new Statement(3000L,1,this.slave.getLowerFirstPlural()+" := []"+this.slave.getDomainSuffix()+"."+this.slave.getCapFirstDomainNameWithSuffix()+"{}"));
		sList.add(new Statement(4000L,1,"if "+this.master.getLowerFirstDomainName()+"Id > 0 {"));
		sList.add(new Statement(5000L,2,this.slave.getLowerFirstPlural()+" = "+this.master.getServiceimplSuffix()+"."+StringUtil.capFirst(this.getVerbName())+"("+this.master.getLowerFirstDomainName()+"Id)"));
		sList.add(new Statement(6000L,1,"}"));
		sList.add(new Statement(7000L,1,"total := len("+this.slave.getLowerFirstPlural()+")"));
		sList.add(new Statement(8000L,1,"SendResponse(c, nil, "+this.slave.getLowerFirstPlural()+", total, true)"));
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}

	public ListMyAvailableActive(Domain master,Domain slave){
		super();
		this.master = master;
		this.slave = slave;
		this.setVerbName("ListAvailableActive"+this.master.getCapFirstDomainName()+StringUtil.capFirst(this.slave.getAliasPlural())+"Using"+this.master.getCapFirstDomainName()+"Id");
		this.setLabel("列出可得");
		this.dbType = master.getDbType();
	}
}
